<?php

namespace common\controller;

use common\cache\SubQueue;
use common\exception\ExceptionEmun;
use common\exception\SystemException;
use common\helper\YaconfHelper;
use common\traits\ControllerTrait;
use think\App;
use think\Controller;
use think\facade\Request;

/**
 * Class BaseController
 * @package common\controller
 */
class BaseController extends Controller
{
    use ControllerTrait;

    /**
     * @var array|string
     */
    public $param;
    /**
     * @var array|string
     */
    public $get;
    /**
     * @var array|string
     */
    public $post;

    /**
     * BaseController constructor.
     * @param App|null $app
     */
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $param = Request::param();
        $get = Request::get();
        $post = Request::post();
        $this->param = $this->formatParam($param);
        $this->get = $this->formatParam($get);
        $this->post = $this->formatParam($post);
        $this->app = $app;
    }

    /**
     * 格式化参数
     * @param $param
     * @return array|string
     */
    public function formatParam($param)
    {
        $except_arr = ['create_time', 'update_time', 'creater_id', 'updater_id'];
        if (is_array($param)) {
            foreach ($param as $key => $value) {
                //过滤空字符串
                $param[$key] = $this->formatParam($value);
                //去除自动完成的字符串,这里注意数组的零键和数组相等
                if (in_array($key, $except_arr, true)) {
                    unset($param[$key]);
                }
            }
        } else {
            $param = trim($param);
        }
        return $param;
    }

    /**
     * 写入日志
     * @param $member_info
     */
    protected function logHistory($member_info)
    {
        $request = $this->request;
        $data = [
            'creater_id' => $member_info[$member_info->getPk()],
            'updater_id' => $member_info[$member_info->getPk()],
            'host' => $request->host(),
            'module' => $request->module(),
            'controller' => $request->controller(),
            'method' => $request->action(),
            'action_get' => json_encode($request->get(), JSON_UNESCAPED_UNICODE),
            'action_post' => json_encode($request->post(), JSON_UNESCAPED_UNICODE),
            'create_date' => date("Y-m-d H:i:s"),
        ];

        SubQueue::instance()->pushActionLog($data);
    }

    /**
     * 返回一个代表成功的json数据 <br>['code' => 0, 'msg' => 'success', 'data' => $data]
     * @param array $data
     * @param array $param
     * @return \think\response\Json
     */
    public function json_responce($data = [], $param = [])
    {
        if (is_array($data) || $data instanceof \ArrayAccess) {
            if (!empty($param)) {
                if (!empty($data['param'])) {
                    ExceptionEmun::throwException(SystemException::FIELD_CONFILICT);
                }
                $data['param'] = $param;
            }

        }

        return json([
            'code' => 0,
            'msg' => 'success',
            'data' => $data,
        ]);
    }

}
